package com.woniu.mealsService.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.StoreViewDto;
import com.woniu.mealsService.model.Store;
import com.woniu.mealsService.model.StoreView;
import com.woniu.mealsService.service.StoreService;
import com.woniu.mealsService.service.StoreViewService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.PageInfo;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author jiang
 */
@RestController
@RequestMapping("/storeView")
@Api(tags = "商家面板管理")
public class StoreViewController {
    @Autowired
    private StoreViewService storeViewService;
    @Autowired
    private StoreService storeService;
    @GetMapping("/searchDailyStoreView")
    @ApiOperation(value = "查询日销售数据")
    @RequireRole({"商家"})
    public Result<?> searchStoreView(@RequestHeader String token,@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String date) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        if (!StringUtil.isNotEmpty(date)){
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        Store storeServiceOne = storeService.getOne(new LambdaQueryWrapper<Store>().eq(Store::getUserId, userId));
        StoreViewDto storeViewDto = storeViewService.getCalculateStoreDailySales(storeServiceOne.getStoreId(),date);
        return Result.getResult(StoreViewDto.class).setMsg("查询成功").setCode(200).setData(storeViewDto);
    }
    @GetMapping("/searchMonthStoreView")
    @ApiOperation(value = "查询月每天销量")
    @RequireRole({"商家"})
    public Result<?> searchMonthStoreView(@RequestHeader String token,@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String date) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        if (!StringUtil.isNotEmpty(date)){
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        Store storeServiceOne = storeService.getOne(new LambdaQueryWrapper<Store>().eq(Store::getUserId, userId));
        List<StoreViewDto> storeViewDtos =storeViewService.getCalculateStoreMonthSales(storeServiceOne.getStoreId(),date);
        return Result.getListResult(StoreViewDto.class).setMsg("查询成功").setCode(200).setData(storeViewDtos);
    }
    @GetMapping("/searchPageMonthStoreView")
    @ApiOperation(value = "查询月每天销量")
    @RequireRole({"商家"})
    public Result<?> searchPageMonthStoreView(@RequestHeader String token, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String date, @Min(value = 1, message = "页码最小为1") Integer pageNum,@Min(value = 1, message = "页行数最小为1") Integer pageSize) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        if (!StringUtil.isNotEmpty(date)){
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        Store storeServiceOne = storeService.getOne(new LambdaQueryWrapper<Store>().eq(Store::getUserId, userId));
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        com.github.pagehelper.PageInfo<StoreView> storeViewPageInfo = storeViewService.searchPageMonthStoreView(storeServiceOne.getStoreId(), date, pageNum, pageSize);
        return Result.getResult(com.github.pagehelper.PageInfo.class).setMsg("查询成功").setCode(200).setData(storeViewPageInfo);
    }
}
